package com.swn.common.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;


/**
 * Velocity工具类
 * 
 * @author taozhaocai
 * @Date 2016-5-11  下午2:12:15
 */

public class VelocityUtil{
	private static Logger logger = Logger.getLogger("VelocityUtil.class");
	private static  String docxmlPath = "";//docxml模板路径
	private static String docTempPath = "";//产生的文件路径

	/**
	 * 功能：根据模板文件返回转换后的字符串
	 * 
	 * @param path：目标文件路径（包括文件名称）
	 * @param ctx ：传递变量context
	 * @return
	 */
	public static String fileTempletToStr(String vmname,String filepath, VelocityContext ctx) {
		VelocityEngine ve = null;
		StringWriter sw = null;
		try {
			ve = new VelocityEngine();
			ve.setProperty(ve.FILE_RESOURCE_LOADER_PATH, filepath);

			ve.init();
			Template t = ve.getTemplate(vmname);
			sw = new StringWriter();
			t.merge(ctx, sw);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				sw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sw.toString();

	}

	/**
	 * 功能：根据模板字符串返回转换后的字符串
	 * @param templetStr：模板字符串
	 * @param ctx ：传递变量的context
	 * @return
	 */
	public static String strTempletToStr(String templetStr, VelocityContext ctx) {
		Properties props = new Properties();
		props.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		props.setProperty(Velocity.RESOURCE_LOADER, "class");

		VelocityEngine ve = null;
		StringWriter sw = null;
		try {
			ve = new VelocityEngine();
			ve.init();
			sw = new StringWriter();
			ve.evaluate(ctx, sw, "", templetStr);
		} catch (Exception e){
			e.printStackTrace();
		}finally {
			try {
				sw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sw.toString();
	}
	
	
	/**
	 * 根据模板生成word
	 * 
	 * @param context
	 * @param templateName：模板名称，包括扩展名，但不包括路径
	 * @param docName：生成提供下载的文件名称。如：xxx.doc
	 * @return String:则返回生成word的路径，否则返回空字符串
	 *
	 * @Author taozhaocai
	 * @Date 2016-5-11 下午2:13:34 
	 */
	public static String createDoc(VelocityContext context,String templateName,String docName){
		String returnPath = "";
		OutputStreamWriter fw = null;
		FileOutputStream  fo = null;
		File docFile = null;
		/**根据模板创建文件**/
		VelocityEngine ve = new VelocityEngine();
		
		Properties p = new Properties();
		p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, VelocityUtil.getDocxmlPath());
		
		try {
			ve.init(p);
			// 加载模板，设定模板编码
			Template t = ve.getTemplate(templateName, "UTF-8");
			docFile = new File(docName);
			fo = new FileOutputStream(VelocityUtil.getDocTempPath()+docFile);
			fw = new OutputStreamWriter(fo,"UTF-8");
			t.merge(context, fw);
			fw.flush();	
			
			returnPath = VelocityUtil.getDocTempPath()+docName ;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fo!=null){
				try {
					fo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fw!=null){
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return returnPath;
	
	}
	

	public static String getDocxmlPath() {
		if(docxmlPath.length()==0){
			docxmlPath = PropertiesTools.getValueByKey("sys.properties", "docxmlPath");
			//FileUtil.createFolder(docxmlPath);
		}
		return docxmlPath;
	}

	public static void setDocxmlPath(String docxmlPath) {
		VelocityUtil.docxmlPath = docxmlPath;
	}

	public static String getDocTempPath() {
		if(docTempPath.length()==0){
			docTempPath = PropertiesTools.getValueByKey("sys.properties", "docTempPath");
			FileUtil.createFolder(docTempPath);
		}
		
		return docTempPath;
	}

	public static void setDocTempPath(String docTempPath) {
		VelocityUtil.docTempPath = docTempPath;
	}

	public static void main(String[] args) throws Exception {
		try {
			 //dd();
			String s = FileUtil.readFile("d:\\2.xml");
			VelocityContext ctx = new VelocityContext();
			strTempletToStr(s,ctx);
			System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
